JBoss Community Archive (Read Only)

Infinispan 6.0

Dynamically Start and Stop Clustered Cache

Library Mode

Clustered

Start start/stop cache in non-clustered mode is simple.  You can use EmbeddedCacheManager.defineConfiguration(cacheName, configuration) to define a cache, and then call EmbeddedCacheManager.getCache(cacheName).

If you don't define a specific configuration for the cache and directly call EmbeddedCacheManager.getCache(...), then a new cache would be created with default configurations.

To stop a cache, call EmbeddedCacheManager.remove(cacheName);

Clustered

To start a clustered cache, you'll need to do the above on every clustered node, while making sure the cache mode is clustered, of course.

You can start the cache by calling _EmbeddedCacheManager.getCache(...).  _To do this on every single node though, you could write your own service to do that, or with JMX, or use DistributedExecutorService.

For example, write a StartCacheCallable class:

 public class StartCacheCallable<K, V> implements DistributedCallable<K, V, Void>, Serializable {
 private static final long serialVersionUID = 8331682008912636780L;
 private final String cacheName;
 private transient Cache<K, V> cache;


 public StartCacheCallable(String cacheName) {
    this.cacheName = cacheName;
 }

 @Override
 public Void call() throws Exception {
    cache.getCacheManager().getCache(cacheName); // start the cache
    return null;
 }

 @Override
 public void setEnvironment(Cache<K, V> cache, Set<K> inputKeys) {
    this.cache = cache;
 }

}

Then submit the task to all nodes:

DistributedExecutorService des = new DefaultExecutorService(existingCacheSuchAsDefaultCache);
List<Future<Void>> list = des.submitEverywhere(new StartCacheCallable<K, V>(cacheName));
for (Future<Void> future : list) {
   try {
      future.get(); // wait for task to complete
   } catch (InterruptedException e) {
   } catch (ExecutionException e) {
   }
}

Server Mode

Hot Rod client does not support dynamically start/stop of cache.

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-11 09:39:18 UTC, last content change 2013-02-20 17:41:41 UTC.